package com.guanghua.brick.html.tag;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.guanghua.brick.html.IContent;
import com.guanghua.brick.html.IControl;
import com.guanghua.brick.html.TreeConfigContent;

public class ContentTreeConfigTag extends BodyTagSupport {
	
	private static Log logger = LogFactory.getLog(ContentTreeConfigTag.class);
	
	private TreeConfigContent content = new TreeConfigContent();
	
	public int doEndTag() throws JspException {
		//获取content pool，并将content放入
		Map<String, IContent> pool = ContentConfigTag.getContentPool(pageContext.getServletContext());
		pool.put(content.getId(), content);
		logger.trace("content id: " + content.getId());
		logger.trace("content cls: " + content.toString());
		
		//设置父控件中的content属性
		Tag parent = this.getParent();
		if (parent instanceof IControl) {
			((IControl)parent).setContent(content);
		}
		return BodyTagSupport.EVAL_PAGE;
	}

	public String getFile() {
		return content.getFile();
	}

	public String getId() {
		return content.getId();
	}

	public String getParentKeyProperty() {
		return content.getParentKeyProperty();
	}

	public String getPrimaryKeyProperty() {
		return content.getPrimaryKeyProperty();
	}

	public String getTextProperty() {
		return content.getTextProperty();
	}

	public boolean isPermissionAvail() {
		return content.isPermissionAvail();
	}

	public void setFile(String file) {
		HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
		String url = request.getServletPath();
		logger.trace("init tree config request uri" + url);
		int p = url.lastIndexOf("/");
		url = url.substring(0, p+1) + file;
		logger.trace("init tree config content url:" + url);
		url = request.getSession().getServletContext().getRealPath(url);
		logger.trace("init tree config content real path:" + url);
	
		content.setFile(url);
	}

	public void setId(String id) {
		content.setId(ContentConfigTag.getPagePrefix((HttpServletRequest)pageContext.getRequest(), id));
	}

	public void setParentKeyProperty(String parentKeyProperty) {
		content.setParentKeyProperty(parentKeyProperty);
	}

	public void setPermissionAvail(boolean permissionAvail) {
		content.setPermissionAvail(permissionAvail);
	}

	public void setPrimaryKeyProperty(String primaryKeyProperty) {
		content.setPrimaryKeyProperty(primaryKeyProperty);
	}

	public void setTextProperty(String textProperty) {
		content.setTextProperty(textProperty);
	}
	
}
